package com.hluobo.library.action;

import javax.annotation.Resource;
import org.json.JSONObject;

import com.hluobo.library.model.User;
import com.hluobo.library.service.UserService;
import com.hluobo.library.util.StreamTool;
import com.opensymphony.xwork2.ActionSupport;

public class JsonLogin extends ActionSupport {

	private int uid = -1;
	private int resCode = -1;
	private String uname = "err";
	private String msg = "err";
	private UserService us;

	@Override
	public String execute() throws Exception {
		try {
			
			JSONObject jsonObj = StreamTool.readRequstString();

			String username = jsonObj.getString("username");
			String password = jsonObj.getString("password");
System.out.println(username+"--"+password);
			User user = us.login(username, password);
			if (user != null) {
				resCode = 200;
				uid = user.getId();
				uname = user.getUsername();
				msg = "登陆成功";
			} else {
				resCode = 250;
				msg = "用户名或密码错误";
			}
		} catch (Throwable e) {
			resCode = 500;
			msg = "服务器未知错误";
			e.printStackTrace();
		}

		return "success";
	}
	
	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public UserService getUs() {
		return us;
	}

	@Resource(name = "userService")
	public void setUs(UserService us) {
		this.us = us;
	}

	public int getResCode() {
		return resCode;
	}

	public void setResCode(int resCode) {
		this.resCode = resCode;
	}

}
